using SqlSugar;
using IM.Easy.Core.Services;
using IM.Easy.Core.Utils;
using IM.Easy.Infrastructure.Query;
using IM.Easy.Core.Extensions;
using IM.Easy.Entity.Msg;
using IM.Easy.MsgServices.IServices;
using Mapster;
using IM.Easy.MsgServices.Dtos;
using IM.Easy.Core.AppContext;

namespace IM.Easy.MsgServices.Services
{
    public class EmailsService : AppServices<Emails>, IEmailsService
    {

        public EmailsService(ISqlSugarClient db) : base(db)
        {

        }

        /// <summary>
        /// 存草稿
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public Emails Save(EmailsCreateDto entity)
        {
            var email = GetById(entity.Id);
            email = entity.Adapt(email);
            email = InsertOrUpdateReturnEntity(email);
            return email;
        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="emailId"></param>
        /// <returns></returns>
        public Emails Send(long emailId)
        {
            var email = GetById(emailId);
            email.Status = EmailStatus.Sent;
            email.SentTime = DateTime.Now;
            var user = ImApp.User;
            var sender = Context.Queryable<EmailAccount>().Single(s => s.UserId == user.Id);
            email.SenderId = sender.Id;
            var flag = Update(email);
            return email;
        }

        public Emails GetById(int id)
        {
            // TODO: 添加获取逻辑
            return Context.Queryable<Emails>().InSingle(id);
        }

        /// <summary>
        /// 获取邮箱列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public PageResponse<EmailsDto> GetPageList(EmailsQueryDto query)
        {
            var queryable = Context.Queryable<Emails>();
            var exp = Expressionable.Create<Emails>();
            exp = exp.AndIF(!string.IsNullOrEmpty(query.Subject), s => s.Subject.Contains(query.Subject))
                       .AndIF(query.Status != default, s => s.Status == query.Status.Value)
                       .AndIF(query.SenderId != default, s => s.SenderId == query.SenderId);
            var page = queryable
                 .Where(exp.ToExpression())
                 .Select(s => new EmailsDto
                 {
                     Status = s.Status,
                     Subject = s.Subject,
                     UpdatedAt = s.UpdatedAt,
                     Abstract = s.Abstract,
                     Id = s.Id,
                     SentTime = s.SentTime,
                 })
                 .ToPageList(query);
            return page;
        }

        public bool Update(Emails entity)
        {
            // TODO: 添加更新逻辑
            return Context.Updateable(entity).ExecuteCommandHasChange();
        }

        public bool Delete(params int[] Id)
        {
            // TODO: 添加删除逻辑
            return Context.Deleteable<Emails>(Id).ExecuteCommandHasChange();
        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool SendEmail(Emails entity)
        {

            return true;
        }

        /// <summary>
        /// 获取收件人ID
        /// </summary>
        /// <param name="emailId"></param>
        /// <returns></returns>
        public List<long> GetAudinceUserId(long emailId)
        {
            var audinceId = Context.Queryable<EmailRecives>()
               .Where(s => s.EmailId == emailId)
               .Select(s => s.AudienceId).ToList();
            return audinceId;
        }
    }
}
